package com.cgnpc.scp.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class ExceptionUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionUtil.class);

    public static String getErrorMessage(Throwable throwable) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        PrintStream printStream = null;

        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            printStream = new PrintStream(byteArrayOutputStream, true, "UTF-8");
            throwable.printStackTrace(printStream);
            return new String(byteArrayOutputStream.toByteArray(), "UTF-8");
        } catch (Throwable t) {
            LOGGER.error("getErrorMessage.error;", t);
        } finally {
            IOUtil.closeStream(byteArrayOutputStream);
            IOUtil.closeStream(printStream);
        }

        return null;
    }

}
